<?php

require_once("models/facadeModel.php") ;
require_once("views/BaseView.php") ;
require_once("views/webView.php") ;
require_once("views/NavigationView.php") ;
require_once("models/messageModel.php") ;
require_once("models/loginModel.php");
require_once("models/searchModel.php");
require_once("controllers/loginController.php");
require_once("controllers/searchController.php");
require_once("advancedSearchController.php") ;
    

class Controller {

    private $m_view;
	private $m_facade;
	private $m_loginController;
    private $m_searchController ;
    private $m_advancedSearchController ;

    public function __construct() {
        $this->m_view = new WebView() ;
		$this->m_facade = new FacadeModel();
        $searchModel = new SearchModel($this->m_facade) ;
		$this->m_loginController = new LoginController(new LoginModel($this->m_facade));
        $this->m_searchController = new SearchController($searchModel);
        $this->m_advancedSearchController = new AdvancedSearchController($searchModel) ;
    } 

    public function doControll() {

		$return['right_col'] = $this->m_loginController->doControll() ;
		
		$this->m_view->setLoginStatus($this->m_loginController->isUserLoggedIn()) ;
        
        $return['left_col'] = '' ;
		$return['menu'] = $this->m_view->menu() ;

        switch($this->m_view->getView()) {
            case NavigationView::registerMember:
                $return['left_col'] .= $this->m_view->renderMemberForm(NavigationView::addMember) ;
                break ;
            case NavigationView::registerBoat:
                $return['left_col'] .= $this->m_view->renderBoatForm(NavigationView::addBoat) ;
                break ;
            case NavigationView::addMember:
                $return['left_col'] .= $this->m_facade->addMember($this->m_view->getUserInfo()) ;
                break ;
            case NavigationView::addBoat:
                $return['left_col'] .= $this->m_facade->addBoat($this->m_view->getBoatInfo()) ;
                break ;
	    	case NavigationView::listMembersCompact:
				$return['left_col'] .= $this->m_view->renderMembersCompact($this->m_facade->getMembers()) ;
				break ;
	    	case NavigationView::listMembersFull:
				$return['left_col'] .= $this->m_view->renderMembersFull($this->m_facade->getMembers()) ;
                break ;
            case NavigationView::changeMember:
                $return['left_col'] .= $this->m_view->renderMemberForm(NavigationView::saveMember, $this->m_facade->getMember($this->m_view->getMemberId())) ;
                break ;
            case NavigationView::saveMember:
                $return['left_col'] .= $this->m_facade->saveMember($this->m_view->getUserInfo()) ;
                break ;
            case NavigationView::showMember:
				$return['left_col'] .= $this->m_view->showMember($this->m_facade->getMember($this->m_view->getMemberId())) ;
                break ;
            case NavigationView::changeBoat:
                $return['left_col'] .= $this->m_view->renderBoatForm(NavigationView::saveBoat, $this->m_facade->getBoat($this->m_view->getBoatId())) ;
                break ;
            case NavigationView::saveBoat:
                $return['left_col'] .= $this->m_facade->saveBoat($this->m_view->getBoatInfo()) ;
                break ;
            case NavigationView::removeMember:
                $return['left_col'] .= $this->m_facade->removeMember($this->m_view->getMemberId()) ;
                break ;
            case NavigationView::removeBoat:
                $return['left_col'] .= $this->removeBoat($this->m_view->getBoatId()) ;
                break ;
            case NavigationView::search:
                $return['left_col'] .= $this->m_searchController->doControll() ;
                break ;
            default:
      
        }

    
    $return['left_col'] .= $this->m_advancedSearchController->doControll() ;
	$return['messages'] = $this->m_view->showMessages(MessageModel::getMessages()) ;	
	
	echo $this->m_view->generateXhtml($return) ;
    
    }

}
